package com.ls.lserp.modules.material.service;




import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.dao.InventoryRequestExDao;
import com.ls.lserp.modules.material.dao.MaterialOutboundDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.material.entity.MaterialOutInbound;
import com.ls.lserp.modules.order.entity.StyleOptionEntity;
import com.ls.lserp.modules.purchase.entity.PuttingRequest;
@Service
@Transactional(readOnly = false)
public class MaterialOutboundService extends CrudService<MaterialOutboundDao,MaterialOutInbound>{

	//合同
	 @Autowired
	    private MaterialOutboundDao materialOutboundDao ;
	 @Autowired
	    private InventoryRequestExDao inventoryRequestExDao;

	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public Page<MaterialOutInbound> findList(Page<MaterialOutInbound> page, MaterialOutInbound materialOutInbound) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	    	// tab页分，检索条件设定(0:全部,1:待出库,2:已出库)
	    	materialOutInbound.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
			
	        // 设置分页参数
	    	materialOutInbound.setPage(page);
	        // 执行分页查询
	    	//List<MaterialOutInbound> aa = materialOutboundDao.findList(materialOutInbound);
	    	page.setList(materialOutboundDao.findList(materialOutInbound));
	       // page.setList(aa);
	        return page;
	    }
		     
	    /**
	     * @Title: findRequestDetailsList
	     * @Description: 入库申请明细
	     * @param @param inventoryRequest
	     * @param @return
	     * @return List<InventoryRequest>
	     * @throws
	     */
	   
	   public List<MaterialOutInbound> findRequestDetail(MaterialOutInbound materialOutInbound) {
	       return materialOutboundDao.findRequestDetail(materialOutInbound);
	   }
	   /**
	     * @Title: findRequestDetailsList
	     * @Description: 入库申请明细
	     * @param @param inventoryRequest
	     * @param @return
	     * @return List<InventoryRequest>
	     * @throws
	     */
	   
	   public List<MaterialOutInbound> findInventoryMaterialDetailList(MaterialOutInbound materialOutInbound) {
		   materialOutInbound.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
		   materialOutInbound.setInventoryOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
	       return materialOutboundDao.findInventoryMaterialDetailList(materialOutInbound);
	   }
	   /**
	     * @Description: 入库申请明细
	     * @throws
	     */
	   
	   public MaterialOutInbound getInfo(MaterialOutInbound materialOutInbound) {
		   materialOutInbound = materialOutboundDao.getInfo(materialOutInbound);
		   List<MaterialOutInbound> requestDetailList = materialOutboundDao.findRequestDetail(materialOutInbound);
		   materialOutInbound.setRequestDetailsList(requestDetailList);
	       return materialOutInbound;
	   }
	   /**
	    * @Title: saveInventoryRequest
	    * @Description: 保存入库申请
	    * @param @param inventoryRequest
	    * @return void 
	    * @throws
	    */
	   @Transactional(readOnly = false)
	   public void saveMaterialDetail(MaterialOutInbound materialOutInbound) {
	   	// 判断list是否为空
	   	if(!CollectionsUtils.isEmpty(materialOutInbound.getRequestDetailsList())){
	   		materialOutInbound.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
	   		materialOutboundDao.deleteMaterialDetail(materialOutInbound);
	   	   // 遍历赋值
	          for(MaterialOutInbound itr : materialOutInbound.getRequestDetailsList()){
	        	  if (itr.getInventoryQuantity()!=null&&!"".equals(itr.getInventoryQuantity())) {
	        		  itr.preInsert();
	        		  itr.setVersion(1L);
	        		  itr.setDeleteFlag(MaterialOutInbound.DEL_FLAG_NORMAL);
	        		  itr.setEnableFlag(MaterialOutInbound.ENABlE_FLAG_NORMAL);
	        		  itr.setMaterialId(materialOutInbound.getMaterialId());
	        		  itr.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
	        		  itr.setInventoryCategory(materialOutInbound.getInventoryCategory());
	        		  materialOutboundDao.insertDetail(itr);
				}
	   		}
	   	}
	   }
	   /**
		   * @Title: saveDetail
		   * @Description: 保存实际数量
		   * @param @param materialOutInbound
		   * @return void 
		   * @throws
		   */
		  @Transactional(readOnly = false)
		  public void saveQuantity(MaterialOutInbound materialOutInbound) {
			  materialOutInbound.preInsert();
			  materialOutboundDao.updateDetail(materialOutInbound);
			  materialOutboundDao.updateQuantity(materialOutInbound);
		  }
		  /**
		   * @Title: saveDetail
		   * @Description: 更新状态
		   * @param @param materialOutInbound
		   * @return void 
		   * @throws
		   */
		  @Transactional(readOnly = false)
		  public void changeStatus(MaterialOutInbound materialOutInbound) {
			  materialOutInbound.preInsert();
			  materialOutboundDao.changeStatus(materialOutInbound);
		  }
		  
		  /**
			* 获取定制合同小信息 
			*
			* @return
			*/
		  public List<StyleOptionEntity> findContractInfo(){
			 return inventoryRequestExDao.findContractInfo();
		  }
}
